﻿using BouncingBallGame.DataModel;
using BouncingBallGame.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Pickers;

namespace BouncingBallGame.Helpers
{
    public static class FileManager
    {
        public static async Task<GameLevelModel> LoadLocalFile(string fileName)
        {
            StorageFolder installFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder localFilesFolder = await installFolder.GetFolderAsync("DataModel");
            StorageFile file = await localFilesFolder.GetFileAsync(fileName);
            string json = await Windows.Storage.FileIO.ReadTextAsync(file);

            GameLevelModel gameLevel;

            if (LevelsManager.TryGetFromJson(json, out gameLevel))
            {
                return gameLevel;
            }

            return null;
        }

        public static async void SaveJson(GameLevelModel gameLevel, string proposedName = "UntitledLevel1")
        {
            string json = LevelsManager.ToJson(gameLevel);

            var savePicker = new FileSavePicker();

            savePicker.DefaultFileExtension = ".json";
            savePicker.CommitButtonText = "Save game level";
            savePicker.SuggestedFileName = proposedName;
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

            var plainTextFileTypes = new List<string>(new string[] { ".json", ".txt" });

            savePicker.FileTypeChoices.Add(
                new KeyValuePair<string, IList<string>>("Plain Text", plainTextFileTypes)
                );

            var saveFile = await savePicker.PickSaveFileAsync();

            if (saveFile != null)
            {
                try
                {
                    await Windows.Storage.FileIO.WriteTextAsync(saveFile, json);
                    await new Windows.UI.Popups.MessageDialog("File Saved!").ShowAsync();
                }
                catch
                {
                     new Windows.UI.Popups.MessageDialog("File couldn't save!").ShowAsync();
                }
            }
        }

        public static async Task<GameLevelModel> LoadJson()
        {
            var openPicker = new Windows.Storage.Pickers.FileOpenPicker();

            openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

            openPicker.FileTypeFilter.Add("*");

            var file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                try
                {
                    string json = await Windows.Storage.FileIO.ReadTextAsync(file);

                    GameLevelModel gameLevel;

                    if (LevelsManager.TryGetFromJson(json, out gameLevel))
                    {
                        return gameLevel;
                    }

                    throw new ArgumentException("Couldn't load json!");
                }
                catch
                {
                    new Windows.UI.Popups.MessageDialog("File couldn't load!").ShowAsync();
                }
            }

            return null;
        }
    }
}
